/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('ProblemSearchDetailCtrl', ['$scope','$http','$modal','BaseURL','Modal','CaseCommonService','WarningScreenService',
	                                           'FwaTabsetService','AuditDictServices','$stateParams','$state','WarningAddService',
	                                function($scope,$http,$modal,baseUrl,Modal,CaseCommonService,WarningScreenService,
	                                		fwaTabsetService,AuditDictServices,$stateParams,$state,WarningAddService){
		
		
		
		$scope.problemProcessOpinion={}; // 处理意见
		$scope.problemAuditOpinion={}; // 审核意见
		$scope.surveyResult={}; // 调查详情
		$scope.surveyReview={}; // 调查复核
		$scope.surveyAudit={}; // 调查审核
		$scope.auditDictServices = AuditDictServices;
        AuditDictServices.initCodeListDatas();
		
        $scope.problemStatus = 4;
		$scope.dispatchButton=true;
		$scope.confirmButton=true;
		
		$scope.finishNode = 7;
		
		$scope.object={}; //审核对象
		
		var tabSource = [{label:'事件处理',id:0,status:1,url:'views/hcpms/audit/problemSearch/caseCheckProblemDeal.html'},
  		             {label:'调查结果',id:1,status:5,url:'views/hcpms/audit/problemSearch/caseCheckSurveyResult.html'}];
		
        $scope.problemtab=[];

        $scope.tabClicks = function(tab) {
            $scope.selectedTabs = tab;
            $scope.tabIndex = tab.id;
            setTimeout(function(){
				$(window).trigger('resize');
            },0);
        }
//        $scope.tabClicks($scope.problemtab[4]);
        
       $scope.tableData = [];
       
       
          //初始化请求参数
	    function params(){
			var problemCode = "";
			if($stateParams.problemCode && !angular.isUndefined($stateParams.problemCode) ){
				problemCode = $stateParams.problemCode;
				$scope.problemCode=$stateParams.problemCode;
			}
			var params = {
					problemNo:problemCode
			}
			return params;
		}
       
	    /**
	     * 是否展示复核意见
	     */
	    $scope.chuckOpione = 0;
	    
       
       
       /**
        * 初始化医疗机构,医务人员,参保人信息
        */
       function initMedicalAndStaffAndInsurance(){
    	   var param=params();
			CaseCommonService.loadMedicalInfoList(param).success(function(response){   	 
				$scope.medicalData2=response.medicalBean.medicalList;
				$scope.staffData2=response.staffBean.staffList;
				$scope.insuranceData2=response.insuranceBean.insuranceList;
				WarningScreenService.findPointCutTempScose(
						WarningAddService.warningMarks,$stateParams.problemCode,'02',
	     				  $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
//				CaseCommonService.initStaffListWithMarks($scope,$stateParams.problemCode);
			});
       }
       
       /**
        * 初始化各个tab 信息
        */
       function initTabInfo(){
    	   var param = {caseCode:$stateParams.caseCode,problemNo:$stateParams.problemCode};
    	   CaseCommonService.queryAllOpinion(param).success(function(response){   	 
    		   resetEachTabData(response);
			});
       }
       
   	/**
      * 0-初始任务分配
      * 1-调查初审 
      * 2-事件审核
      * 3-调查结论
      * 4-调查结论复核 
      * 5-调查结论审核
   	 */
       
       
       function resetEachTabData(_array){
    	   if(_array!= null && _array.length >0){
    		   angular.forEach(_array,function(data,index){
    			   if(data.opinionType == 1){
    				   $scope.problemProcessOpinion = data;
    			   }else if(data.opinionType == 2){
    				   $scope.problemAuditOpinion = data;
    				   if(data.opinion !=1 ){
						   $scope.finishNode = 3;// 结束节点是 审核
					   }
    			   }else if(data.opinionType == 3){
    				   $scope.surveyResult = data;
    			   }else if(data.opinionType == 4){
    				   $scope.surveyReview = data;
    			   }else if(data.opinionType == 5){
    				   $scope.surveyAudit = data;
    			   }
    		   });
    	   }
    	   if(!angular.isUndefined($stateParams.problemStatus) && $stateParams.problemStatus){
				if($stateParams.problemStatus >5 && $scope.finishNode >3){
					$scope.problemtab = tabSource;
					$scope.tabClicks($scope.problemtab[1]);
				}else if($stateParams.problemStatus > 1 ){
					$scope.problemtab.push(tabSource[0]);
					$scope.tabClicks($scope.problemtab[0]);
				}
				$scope.problemStatus = $stateParams.problemStatus;
			}
    	   
       }
       
       /**
        * 初始化所有的数据
        */
       function initAllData(){
    	  initMedicalAndStaffAndInsurance();
    	  initTabInfo();
    	  initProblemData();
       }
       
		function initProblemData(){
			var parameter=params();
			CaseCommonService.loadProblemData(parameter).success(function(response){
		    	 var obj=response.problemObj;
		    	 $scope.object=obj;	
			});
		}
		 
        
		
       /**
        * 返回事件
        */
       $scope.goBack = function(){
    	   $state.go('problemSearch');
       }
       
       /**
        * 查看事件对象的案件来源对话框信息
        */
       $scope.$on('fwaAuditLeft.showModal',function(){
    	   $scope.problemObj=$scope.object;
    	   CaseCommonService.showProblemFromSourceDialog($scope);
       });
       
       
       
       /**
        * 点击医务人员总数量事件
        */
       $scope.$on('fwaInvolve.staffCountClick',function(){
    	   	$scope.caseNo = $stateParams.problemCode;
       		CaseCommonService.viewStaffList($scope);
       });
       
       /**
        * 点击参保人总数量事件
        */
       $scope.$on('fwaInvolve.insuranceCountClick',function(){
       	CaseCommonService.viewInsurList($scope);
       });

       /**
        * 点击医疗机构总数量事件
        */
       $scope.$on('fwaInvolve.medicalCountClick',function(){
    	   CaseCommonService.viewMedicalDetail($scope);
       });
       
       
       initAllData();
       
       // 	end line	
	}]);
})();